<template>
  <div id="attribute_list">
    <div class="search-box">
      <Row type="flex">
        <Row type="flex" justify="start" class="search-item-left">
          <Col class="search-item"><span class="search-label">输入搜索：</span><Input placeholder="属性编码/属性名称" v-model='searchName'></Input></Col>
          <Col class="search-item">
            <Button class="search-btn" type='primary' @click="doSearch">查询</Button>
          </Col>
          <Col class="search-item">
            <Button class="search-btn" @click="reset">重置</Button>
          </Col>
        </Row>
        <Row type="flex" justify="end" class="search-item-right">
          <Col class="search-item"><Button class="search-btn" type="primary" style="margin-right:8px" @click="skipAdd" v-if="hasPermission('moms:product:propcreate')">添加属性</Button></Col>
        </Row>
      </Row>
    </div>
    <Table stripe class="table-list" :columns="columns" :data="tableData" :loading="loading" :height="tableHeight">
      <template slot-scope="{ row }" slot="action">
        <i class="iconsys iconsetting" title='参数列表' @click='skipSet(row)' v-if="hasPermission('moms:product:paramview')"></i>
        <i class="iconsys iconedit" title="编辑" @click='skipEdit(row)' v-if="hasPermission('moms:product:propupdate')"></i>
        <Poptip confirm title="确认删除此数据吗？" transfer @on-ok="sureDel(row)" style="text-align:left">
          <i class="iconsys icondel" title="删除" v-if="hasPermission('moms:product:propdelete')"></i>
        </Poptip>
      </template>
    </Table>
    <Row type='flex' justify="end" style='padding: 20px'>
      <Col>
        <Page :total="pageObj.total"
              :page-size='pageObj.pageSize'
              :current='pageObj.pageIndex'
              :page-size-opts='[20,50,100,200]'
              show-total
              show-sizer
              @on-change='changePageIndex'
              @on-page-size-change='changePageSize' />
      </Col>
    </Row>
  </div>
</template>

<script>
import { getAttributeList, delAttribute } from '@/api/commondity/index'
export default {
  name: 'attribute_list',
  computed: {
    tableHeight () {
      let height = this.tableData.length > 10 ? 520 : 0
      return height
    }
  },
  watch: {
    '$route' (newRoute) {
      if (newRoute.name === 'attribute_list') {
        this.getData()
      }
    }
  },
  data () {
    return {
      loading: false,
      searchName: '',
      searchObj: {
        searchName: ''
      },
      pageObj: {
        total: 0,
        pageIndex: 1,
        pageSize: 20
      },
      tableData: [],
      columns: [
        // {
        //   title: '排序',
        //   key: 'order',
        //   sortable: true
        // },
        {
          title: '编号',
          key: 'packageId'
        },
        {
          title: '属性名称',
          key: 'packageName'
        },
        {
          title: '参数属性',
          key: 'countPropName'
        }
        // {
        //   title: '操作',
        //   slot: 'action',
        //   width: 200
        // }
      ]
    }
  },
  methods: {
    doSearch () {
      this.searchObj.searchName = this.searchName
      this.pageObj.pageIndex = 1
      this.getData()
    },
    reset () {
      this.searchObj.searchName = ''
      this.searchName = ''
      this.pageObj.pageIndex = 1
      this.pageObj.pageSize = 20
      this.getData()
    },
    changePageIndex (index) {
      this.pageObj.pageIndex = index
      this.getData()
    },
    changePageSize (size) {
      this.pageObj.pageSize = size
      this.pageObj.pageIndex = 1
      this.getData()
    },
    skipAdd () {
      this.$router.push({ name: 'add_attribute' })
    },
    skipSet (row) {
      this.$router.push({ name: 'paramter_list', params: { id: row.packageId } })
    },
    skipEdit (row) {
      this.$router.push({ name: 'edit_attribute', params: { id: row.packageId } })
    },
    sureDel (row) {
      delAttribute({
        merchantId: this.$store.state.user.merchantId,
        packageId: row.packageId
      }).then(res => {
        if (res.data.status === 200) {
          this.$Message.success('删除成功!')
          this.getData()
        } else {
          this.$Message.error(`${res.data.error}`)
        }
      })
    },
    getData () {
      this.loading = true
      getAttributeList({
        merchantId: this.$store.state.user.merchantId,
        searchKey: this.searchObj.searchName,
        offset: this.pageObj.pageSize * (this.pageObj.pageIndex - 1),
        limit: this.pageObj.pageSize
      }).then(res => {
        this.loading = false
        const result = res.data || {}
        if (result.status === 200) {
          this.tableData = result.data || []
          this.pageObj.total = result.totalCount
          if (result.data.length / this.pageObj.pageSize === 0) {
            if (this.pageObj.pageIndex > 1) {
              this.pageObj.pageIndex--
              this.getData()
            }
          }
        } else {
          this.$Message.error(`${result.error}`)
        }
      }).catch(err => {
        this.loading = false
        this.$Message.error(`${err}`)
      })
    }
  },
  mounted () {
    this.getData()
    if (this.hasPermission(['moms:product:paramview', 'moms:product:propupdate', 'moms:product:propdelete'])) {
      this.columns.push({
        title: '操作',
        slot: 'action',
        width: 200
      })
    }
  }
}
</script>
